package cn.hdu.liang.concurrency;

class DualSynch {
    private Object syncObject = new Object();

    public synchronized void f() {
        for(int i = 0; i < 5; i++) {
            System.out.println("f()");
            try {
                Thread.sleep(3);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
            Thread.yield();
        }
    }

    public void g() {
        synchronized(syncObject) {
            for(int i = 0; i < 5; i++) {
                System.out.println("g()");
                try {
                    Thread.sleep(3);
                } catch(InterruptedException e) {
                    e.printStackTrace();
                }
                Thread.yield();
            }
        }
    }
}

public class SyncObject {
    public static void main(String[] args) {
        final DualSynch ds = new DualSynch();
        new Thread(ds::f).start();
        ds.g();
    }
}
